home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Workfile:: Example.c $
- * $Revision:: 5 $
- *
- * $Author:: Buck Rogers $
- * $Modtime:: 11.03.1998 04:34 Uhr $
- *
- * $History:: Example.c $
- *
- * ***************** Version 5 *****************
- * User: Buck Rogers Date: 11.03.1998 Time: 04:34 Uhr
- * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
- * example plugin now demonstrates the use of the new controls
- *
- * ***************** Version 4 *****************
- * User: Buck Rogers Date: 29.10.1997 Time: 17:42 Uhr
- * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
- * example plugin now makes use of the new 'errorText' variable
- *
- * ***************** Version 3 *****************
- * User: Buck Rogers Date: 08.10.1997 Time: 02:03 Uhr
- * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
- * changed lf to cr in list generation so Mac compatible output is created
- *
- * ***************** Version 2 *****************
- * User: Buck Rogers Date: 05.10.1997 Time: 19:24 Uhr
- * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
- * Added comments related to the new name, company and numCopies items in the
- * parameter block
- *
- * ***************** Version 1 *****************
- * User: Buck Rogers Date: 30.09.1997 Time: 18:31 Uhr
- * Created in $/BSNG/Plugins/BSNG SDK/Example Plugin
- * Adding subproject 'BSNG' to '$/'
- *
- * $NoKeywords:: $
- */
-
-
- #include <MixedMode.h>
- #include <A4Stuff.h>
-
- #include "standard utils.h"
- #include "UltraU.h"
- #include "BSNG API.h"
-
-
- /* don't touch this __procinfo definition or the calls from native code will crash the machine */
-
- ProcInfoType __procinfo = kThinkCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(BSNGParamBlockPtr)));
-
-
- /* function prototypes for ANSI C */
-
- static void DoInit(BSNGParamBlockPtr inData);
- static Boolean DoValidate(BSNGParamBlockPtr inData);
- static void DoCalc(BSNGParamBlockPtr inData);
- static void DoRandomCalc(BSNGParamBlockPtr inData);
- static void DoItemHit(BSNGParamBlockPtr inData);
- static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData);
- static void DoCleanup(BSNGParamBlockPtr inData);
- void main(BSNGParamBlockPtr inData);
-
-
- /* here is where the fun starts :-) */
-
- static void DoInit(BSNGParamBlockPtr inData)
- {
- StringPtr aStr = nil;
-
-
- /* Initialize the Ultra Random Number generator (optional, use MacOS random functions otherwise) */
-
- Ultra_seed1 = inData->randSeed1;
- Ultra_seed2 = inData->randSeed2;
- Ultra_Init();
-
- /* Give the BSNG App the informations if your plugin can do random calculations and if it can write
- to the serial number list */
-
- inData->wantsRandomButton = true;
- inData->canAddToSNList = true;
-
- /* Initialize the EditFields with initial default values (optional, they are empty otherwise) */
- /* if you create a name-based generator please make sure that you copy the name, company and/or numCopies */
- /* values into the corrosponding fields */
-
- aStr = inData->itemValue[kItemValue1];
- BlockMoveData("\p1234", aStr, 5);
-
- aStr = inData->itemValue[kItemValue2];
- BlockMoveData("\p2345", aStr, 5);
-
- aStr = inData->itemValue[kItemValue3];
- BlockMoveData("\p3456", aStr, 5);
-
- aStr = inData->itemValue[kItemValue4];
- aStr[0] = 0;
-
- /* Set the default popup menu item to "+" */
-
- inData->selectedPopupItem[kPopupMenu1] = 1;
-
- /* Uncheck the "Negate Result" checkbox */
-
- inData->checkboxSelected[kCheckbox1] = false;
-
- /* Set the default radio button (+) and disable the others in that radio button group */
-
- inData->radioSelected[kRadioButton1] = true; // "+"
- inData->radioSelected[kRadioButton2] = false; // "-"
- inData->radioSelected[kRadioButton3] = false; // "*"
- inData->radioSelected[kRadioButton4] = false; // "/"
-
- /* Tell the BSNG App if the initialisation was successful (it was in our case) */
-
- inData->errorText[0] = 0;
- inData->error = errExtNoErr;
- inData->errorInItem = 0;
- } /* DoInit */
-
-
- static Boolean DoValidate(BSNGParamBlockPtr inData)
- {
- StringPtr num1Str = inData->itemValue[kItemValue1];
- StringPtr num2Str = inData->itemValue[kItemValue2];
- StringPtr num3Str = inData->itemValue[kItemValue3];
-
- Str31 inputStr = "\pThe input for ";
- Str31 tooLongStr = "\p is too long! ";
- Str31 emptyStr = "\p is empty! ";
- Str63 pleaseStr = "\p\rPlease enter a number between 1 and 9999.";
-
- Boolean error = false;
-
-
- /* we do some simple checks here if the edit fields are not empty or not longer than 8 characters, additionally we could
- also check if the values are really only numbers, but I skipped that because the input filter for the edit fileds
- was set to integer anyway (defined in Constructor) */
-
- inData->errorText[0] = 0;
-
- if ((num1Str[0] < 1) || (num1Str[0] > 4))
- {
- /* If something was wrong in the input tell the BSNG App what edit field contained wrong values */
-
- inData->errorInItem = kEditItem1;
- error = true;
-
- /* Build the errorText string (optional), leave errorText empty if you don't want to use this feature */
-
- myAppendPStr(inData->errorText, inputStr);
- myAppendPStr(inData->errorText, "\pNumber 1");
-
- if (num1Str[0] < 1)
- {
- myAppendPStr(inData->errorText, emptyStr);
- }
- else
- {
- myAppendPStr(inData->errorText, tooLongStr);
- }
-
- myAppendPStr(inData->errorText, pleaseStr);
- }
- else if ((num2Str[0] < 1) || (num2Str[0] > 4))
- {
- inData->errorInItem = kEditItem2;
- error = true;
-
- myAppendPStr(inData->errorText, inputStr);
- myAppendPStr(inData->errorText, "\pNumber 2");
-
- if (num2Str[0] < 1)
- {
- myAppendPStr(inData->errorText, emptyStr);
- }
- else
- {
- myAppendPStr(inData->errorText, tooLongStr);
- }
-
- myAppendPStr(inData->errorText, pleaseStr);
- }
- else if ((num3Str[0] < 1) || (num3Str[0] > 4))
- {
- inData->errorInItem = kEditItem3;
- error = true;
-
- myAppendPStr(inData->errorText, inputStr);
- myAppendPStr(inData->errorText, "\pNumber 3");
-
- if (num3Str[0] < 1)
- {
- myAppendPStr(inData->errorText, emptyStr);
- }
- else
- {
- myAppendPStr(inData->errorText, tooLongStr);
- }
-
- myAppendPStr(inData->errorText, pleaseStr);
- }
-
- if (error)
- {
- inData->error = errExtIncorrectValue;
- return (false);
- }
-
- inData->error = errExtNoErr;
-
- return (true);
- } /* DoValidate */
-
-
- static void DoCalc(BSNGParamBlockPtr inData)
- {
- StringPtr num1Str = inData->itemValue[kItemValue1];
- StringPtr num2Str = inData->itemValue[kItemValue2];
- StringPtr num3Str = inData->itemValue[kItemValue3];
- StringPtr addResultStr = inData->itemValue[kItemValue4];
-
- long result = 0L, value = 0L;
-
-
- /* Convert the first 2 numbers into longs */
-
- StringToNum(num1Str, &result);
- StringToNum(num2Str, &value);
-
- /* Check the selected popup menu item and calculate with the corresponding operator */
-
- switch (inData->selectedPopupItem[kPopupMenu1])
- {
- case (1):
- result += value;
- break;
-
- case (2):
- result -= value;
- break;
-
- case (3):
- result *= value;
- break;
-
- case (4):
- result /= value;
- break;
-
- default:
- break;
- }
-
- /* Convert the 3rd number into a long */
-
- StringToNum(num3Str, &value);
-
- /* Check the radio buttons to get the selected one and calculate with the corresponding operator */
-
- if (inData->radioSelected[kRadioButton1])
- {
- result += value;
- }
- else if (inData->radioSelected[kRadioButton2])
- {
- result -= value;
- }
- else if (inData->radioSelected[kRadioButton3])
- {
- result *= value;
- }
- else if (inData->radioSelected[kRadioButton4])
- {
- result /= value;
- }
-
- /* Check if the "Negate Result" checkbox is selected/checked and if so negate the result */
-
- if (inData->checkboxSelected[kCheckbox1])
- {
- result = -result;
- }
-
- /* Convert the result into a Pascal string */
-
- NumToString(result, addResultStr);
-
- /* Put the result on the Clipboard so users can copy and paste the serial number into the app they want to register */
-
- addResultStr[addResultStr[0] + 1] = '\0';
-
- ZeroScrap();
- PutScrap(addResultStr[0], 'TEXT', &(addResultStr[1]));
- } /* DoCalc */
-
-
- static void DoRandomCalc(BSNGParamBlockPtr inData)
- {
- StringPtr num1Str = inData->itemValue[kItemValue1];
- StringPtr num2Str = inData->itemValue[kItemValue2];
- StringPtr num3Str = inData->itemValue[kItemValue3];
-
- unsigned short i = 0;
-
-
- /* Create your own random numbers as the random functions are not dependent on user input */
-
- num1Str[0] = 0; /* empty the string */
- myBaseBRandomPStr(num1Str, 10, 4); /* generate 8 random digits */
-
- num2Str[0] = 0;
- myBaseBRandomPStr(num2Str, 10, 4);
-
- num3Str[0] = 0;
- myBaseBRandomPStr(num3Str, 10, 4);
-
- /* Now we "randomize" the operators */
-
- inData->selectedPopupItem[kPopupMenu1] = (Ultra_short8u() % 4) + 1; // % 4 because we have 4 menu item entries in our
- // popup, + 1 because the first menu item entry
- // is equal to 1, not 0
-
- for (i = kRadioButton1; i <= kRadioButton4; i++) // deselect all 4 radio buttons in our radio group
- {
- inData->radioSelected[i] = false;
- }
-
- inData->radioSelected[Ultra_short8u() % 4] = true; // then select/activate a random one
- inData->checkboxSelected[kCheckbox1] = (Ultra_short1() ? true : false); // select/check our checkbox randomly
-
- /* Now we are prepared and can call our Calc algorithm */
-
- DoCalc(inData);
- } /* DoRandomCalc */
-
-
- static void DoItemHit(BSNGParamBlockPtr inData)
- {
- switch (inData->itemMessage)
- {
- case (200001):
-
- /* The popup menu was opened and closed */
-
- DoCalc(inData);
- break;
-
- case (300000):
-
- /* The "About this plugin" button was pressed, do whatever you want, I just display a small Alert here */
-
- NoteAlert(1000, nil);
- break;
-
- case (400001):
-
- /* The checkbox was checked/unchecked */
-
- DoCalc(inData);
- break;
-
- case (500001):
- case (500002):
- case (500003):
- case (500004):
-
- /* One of the radio buttons was pressed */
-
- DoCalc(inData);
- break;
-
- default:
- break;
- }
- } /* DoItemHit */
-
-
- static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData)
- {
- StringPtr num1Str = inData->itemValue[kItemValue1];
- StringPtr num2Str = inData->itemValue[kItemValue2];
- StringPtr num3Str = inData->itemValue[kItemValue3];
- StringPtr addResultStr = inData->itemValue[kItemValue4];
-
- long count = 0L;
- short i = 0;
- OSErr err = noErr;
-
-
- /* Write your header to the number list */
-
- count = 28L;
- err = FSWrite(inData->outputRefNum, &count, "BSNG SDK Example Plugin 2.5\r");
-
- /* Return false if an error occured, the creation of list will then be completly aborted */
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = 28L;
- err = FSWrite(inData->outputRefNum, &count, "===========================\r");
-
- if (err != noErr)
- {
- return (false);
- }
-
- /* Create numOfListNumbers random serial numbers and write them to the list */
- /* if you create a name-based generator please make sure that you create at least one number */
- /* using the name, company and/or numCopies informations. You could create more name-based numbers */
- /* by using your own name/company database */
-
- for (i = 0; i < inData->numOfListNumbers; i++)
- {
- DoRandomCalc(inData);
-
- if (inData->checkboxSelected[kCheckbox1])
- {
- count = 3L;
- err = FSWrite(inData->outputRefNum, &count, "-((");
- }
- else
- {
- count = 3L;
- err = FSWrite(inData->outputRefNum, &count, " (");
- }
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = (long) num1Str[0];
- err = FSWrite(inData->outputRefNum, &count, &(num1Str[1]));
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = 3L;
-
- switch (inData->selectedPopupItem[kPopupMenu1])
- {
- case (1):
- err = FSWrite(inData->outputRefNum, &count, " + ");
- break;
-
- case (2):
- err = FSWrite(inData->outputRefNum, &count, " - ");
- break;
-
- case (3):
- err = FSWrite(inData->outputRefNum, &count, " * ");
- break;
-
- case (4):
- err = FSWrite(inData->outputRefNum, &count, " / ");
- break;
- }
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = (long) num2Str[0];
- err = FSWrite(inData->outputRefNum, &count, &(num2Str[1]));
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = 1L;
- err = FSWrite(inData->outputRefNum, &count, ")");
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = 3L;
-
- if (inData->radioSelected[kRadioButton1])
- {
- err = FSWrite(inData->outputRefNum, &count, " + ");
- }
- else if (inData->radioSelected[kRadioButton2])
- {
- err = FSWrite(inData->outputRefNum, &count, " - ");
- }
- else if (inData->radioSelected[kRadioButton3])
- {
- err = FSWrite(inData->outputRefNum, &count, " * ");
- }
- else if (inData->radioSelected[kRadioButton4])
- {
- err = FSWrite(inData->outputRefNum, &count, " / ");
- }
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = (long) num3Str[0];
- err = FSWrite(inData->outputRefNum, &count, &(num3Str[1]));
-
- if (err != noErr)
- {
- return (false);
- }
-
- if (inData->checkboxSelected[kCheckbox1])
- {
- count = 1L;
- err = FSWrite(inData->outputRefNum, &count, ")");
- }
-
- count = 3L;
- err = FSWrite(inData->outputRefNum, &count, " = ");
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = (long) addResultStr[0];
- err = FSWrite(inData->outputRefNum, &count, &(addResultStr[1]));
-
- if (err != noErr)
- {
- return (false);
- }
-
- count = 1L;
- err = FSWrite(inData->outputRefNum, &count, "\r");
-
- if (err != noErr)
- {
- return (false);
- }
- }
-
- /* Don't forget this last return */
-
- count = 1L;
- err = FSWrite(inData->outputRefNum, &count, "\r");
-
- if (err != noErr)
- {
- return (false);
- }
-
- return (true);
- } /* DoAddRandomsToList */
-
-
- static void DoCleanup(BSNGParamBlockPtr inData)
- {
- #pragma unused (inData)
-
- /* We didn't allocate any memory in DoInit, so we can leave this empty, otherwise it would be a VERY good idea
- to deallocate/dispose etc. everything we allocated during our work. If you don't do that we have a nice memory leak */
-
- } /* DoCleanup */
-
-
- void main(BSNGParamBlockPtr inData)
- {
- #if (!GENERATINGPOWERPC)
- EnterCodeResource();
- #endif
-
- /* The message dispatcher */
-
- switch(inData->theMessage)
- {
- case (msgExtInit):
- DoInit(inData);
- break;
-
- case (msgExtCalcHit):
-
- if (DoValidate(inData))
- {
- DoCalc(inData);
- }
-
- break;
-
- case (msgExtRandomHit):
- DoRandomCalc(inData);
- break;
-
- case (msgExtCreateRandom):
-
- /* Report to the BSNG App if your list entry was written ok or if we had an error */
-
- if (DoAddRandomsToList(inData))
- {
- inData->error = errExtNoErr;
- }
- else
- {
- inData->error = errExtWritingToList;
- }
-
- break;
-
- case (msgExtItemHit):
- DoItemHit(inData);
- break;
-
- case (msgExtCleanup):
- DoCleanup(inData);
- break;
-
- default:
- break;
- }
-
- #if (!GENERATINGPOWERPC)
- ExitCodeResource();
- #endif
- } /* main */
-